package com.qu.waibao.my.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.baidu.ShareTypeManager.ShareTypeManager;
import com.qu.waibao.base.BaseFragment;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.my.activity.AcceptOrderActivity;
import com.qu.waibao.my.activity.AccountManagerActivity;
import com.qu.waibao.my.activity.AuthenticationActivity;
import com.qu.waibao.my.activity.BalanceActivity;
import com.qu.waibao.my.activity.CouponActivity;
import com.qu.waibao.my.activity.HelpActivity;
import com.qu.waibao.my.activity.HexiaoActivity;
import com.qu.waibao.my.activity.JifenActivity;
import com.qu.waibao.my.activity.PersonInfoActivity;
import com.qu.waibao.my.activity.PublishOrderActivity;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * @描述 describe
 * @作者 Administrator
 * @创建时间 2018/7/25 17:10
 * @邮箱 1123599561@qq.com
 */
public class ChezhuFragment extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_publish)
    ImageView iv_publish;
    private Intent intent;
    @BindView(R.id.iv_photo)
    ImageView iv_photo;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_acoount)
    TextView tv_acoount;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("com.qu.waibao.SHARE")){
                shareSuccess();
            }
        }
    };
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_chezhu;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        ll_back.setVisibility(View.GONE);
        tv_title.setText("我的");
        iv_publish.setImageResource(R.drawable.wode_fenxiang);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.qu.waibao.SHARE");
        getActivity().registerReceiver(receiver,intentFilter);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @OnClick({R.id.rl_userinfo,R.id.tv_publish_more,
            R.id.tv_unpaid,R.id.tv_qiang,R.id.tv_finishing_p,R.id.tv_finished_p,R.id.rl_coupon,R.id.rl_balance,
            R.id.rl_jifen,R.id.rl_account,R.id.rl_help,R.id.iv_publish})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_publish: //分享
                NiceDialog.init()
                        .setLayoutId(R.layout.share_dialog_layout)     //设置dialog布局文件
                        .setConvertListener(new ViewConvertListener() {     //进行相关View操作的回调
                            @Override
                            public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                                holder.setOnClickListener(R.id.tv_weixin, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Platform platform = ShareSDK.getPlatform(Wechat.NAME);
                                        ShareTypeManager manager = new ShareTypeManager(getActivity(),platform);
                                        manager.shareWebPage();
                                        dialog.dismiss();
                                    }
                                });
                                holder.setOnClickListener(R.id.tv_peng ,new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
                                        ShareTypeManager manager = new ShareTypeManager(getActivity(),platform);
                                        manager.shareWebPage();
                                        dialog.dismiss();
                                    }
                                });
                                holder.setOnClickListener(R.id.tv_weibo, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.dismiss();
                                    }
                                });
                                holder.setOnClickListener(R.id.tv_qq, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Platform platform = ShareSDK.getPlatform(QQ.NAME);
                                        ShareTypeManager manager = new ShareTypeManager(getActivity(),platform);
                                        manager.shareWebPage();
                                        dialog.dismiss();
                                    }
                                });
                                holder.setOnClickListener(R.id.tv_cancel, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dialog.dismiss();
                                    }
                                });
                            }
                        })
                        .setDimAmount(0.3f)     //调节灰色背景透明度[0-1]，默认0.5f
                        .setShowBottom(true)     //是否在底部显示dialog，默认flase
                        .setOutCancel(true)     //点击dialog外是否可取消，默认true
                        .show(getChildFragmentManager());     //显示dialog
                break;
            case R.id.rl_userinfo:
                intent = new Intent(mContext, PersonInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_publish_more:
                intent = new Intent(mContext, PublishOrderActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_unpaid:
                intent = new Intent(mContext, PublishOrderActivity.class);
                intent.putExtra("type","unpaid");
                startActivity(intent);
                break;
            case R.id.tv_qiang:
                intent = new Intent(mContext, PublishOrderActivity.class);
                intent.putExtra("type","qiang");
                startActivity(intent);
                break;
            case R.id.tv_finishing_p:
                intent = new Intent(mContext, PublishOrderActivity.class);
                intent.putExtra("type","finishing");
                startActivity(intent);
                break;
            case R.id.tv_finished_p:
                intent = new Intent(mContext, PublishOrderActivity.class);
                intent.putExtra("type","finished");
                startActivity(intent);
                break;
            case R.id.rl_coupon:
                intent = new Intent(mContext, CouponActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_balance:
                intent = new Intent(mContext, BalanceActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_jifen:
                intent = new Intent(mContext, JifenActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_account:
                intent = new Intent(mContext, AccountManagerActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_help:
                intent = new Intent(mContext, HelpActivity.class);
                startActivity(intent);
                break;


        }
    }

    public void getUserInfo(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.user_info_get);
        map.put("userid", AppConfig.getString("userid"));
        NetManager.getInstance().request(getActivity(), map, true, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String photo = jsonObject.optString("photo");
                    if(!TextUtils.isEmpty(jsonObject.optString("photo"))){
                        ImageLoader.loadImage(Glide.with(mContext),iv_photo,NetManager.getFullUrl(photo),R.drawable.wd_touxiang,
                                R.drawable.wd_touxiang);
                    }
                    tv_name.setText(jsonObject.optString("name"));
                    tv_acoount.setText("帐号： "+jsonObject.optString("phone"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void shareSuccess(){
        Map<String,String> map = new HashMap<>();
        map.put("action",ActionUtil.aftershare_set);
        map.put("userid",AppConfig.getString("userid"));
        NetManager.getInstance().request(getActivity(), map, false, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
